//
//  ProductList_A_ViewController.m
//  NewApp
//
//  Created by xueyan liu on 6/23/12.
//  Copyright (c) 2012 __MyCompanyName__. All rights reserved.
//

#import "ProductList_A_ViewController.h"
#import "ProductData.h"
#import "ThumbnailGalleryView.h"
#import "SystemInlineViewController.h"

@interface ProductList_A_ViewController ()

@end

@implementation ProductList_A_ViewController
@synthesize dataArray;
@synthesize titleText;

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];

    if (dataArray == NULL || [dataArray count]<1) {
        return;
    }
    
    self.navigationItem.hidesBackButton = YES;
    self.titleLabel.text = titleText;
    //init scrollview
    scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(10.f, 35.f, 300.f, 330.f)];
    //scrollView.backgroundColor = [UIColor purpleColor];
    scrollView.contentSize = CGSizeMake(300.f, 420.f);
    scrollView.showsVerticalScrollIndicator = NO;
    [self.view addSubview:scrollView];
    
    //
    thumbGalleryView = [[ThumbnailGalleryView alloc] initWithFrame:CGRectMake(0.f, 0.f, 300.f, 270.f)];
    [thumbGalleryView setGalleryWithLargeImages:dataArray];
    //thumbGalleryView.backgroundColor = [UIColor purpleColor];
    [scrollView addSubview:thumbGalleryView];
    
    //add click the thumnail event
    for (int i =0; i<[thumbGalleryView.imgsView count]; i++) {
        
        ((UIImageView *)[thumbGalleryView.imgsView objectAtIndex:i]).userInteractionEnabled=YES;
        UITapGestureRecognizer *singleTap =[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(onClickImage:)];
        singleTap.numberOfTouchesRequired = 1; 
        singleTap.numberOfTapsRequired = 1; 
        [((UIImageView *)[thumbGalleryView.imgsView objectAtIndex:i]) addGestureRecognizer:singleTap];
        
    }
    [scrollView release];
	// Do any additional setup after loading the view.
}

- (BOOL) isInRect:(CGPoint)center fromPoint:(CGPoint)start toPoint:(CGPoint)end{

    if ((center.x < end.x && center.y < end.y)&&(center.x > start.x && center.y > start.y) ){
        return YES;
    }
    return NO;
}

- (void) onClickImage:(UITapGestureRecognizer *)sender{
    
    int n = 0;
    
    NSLog(@"Click point:x=%f,y=%f",sender.view.center.x,sender.view.center.y);
    
    while (n<[thumbGalleryView.imgsRect count]) {
                
        ImgRect *imgRect = (ImgRect *)[thumbGalleryView.imgsRect objectAtIndex:n];
        
        NSLog(@"Image range from x=%f,y=%f to x=%f,y=%f ",imgRect.start.x,imgRect.start.y,imgRect.end.x,imgRect.end.y);
        
        if ([self isInRect:sender.view.center fromPoint:imgRect.start toPoint:imgRect.end])
            break;
        else {
            n++;
        }
    }
    
	if (sender.numberOfTouchesRequired == 1 && n < [thumbGalleryView.imgsRect count]) {
        //to send request for the detail info.
        NSLog(@"You just click the iamge ID:%@",((ProductEntity *)[dataArray objectAtIndex:n]).ID);
        SystemInlineViewController *vctrl = [[SystemInlineViewController alloc] init];
        vctrl.ID = ((ProductEntity *)[dataArray objectAtIndex:n]).ID;
        [self.navigationController pushViewController:vctrl animated:YES];
        [vctrl release];
	}
}


- (void)viewDidUnload
{
    [super viewDidUnload];
    // Release any retained subviews of the main view.
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}

@end
